package com.example.newappchildrchinese;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.activeandroid.query.Select;
import com.example.newappchildrchinese.mode.ChineseClass;
import com.example.newappchildrchinese.mode.ChineseCourse;
import com.example.newappchildrchinese.mode.ChineseWord;

import java.util.ArrayList;
import java.util.List;

public class StudyActivity extends AppCompatActivity implements View.OnClickListener {

    private ImageView topbar_ico;
    private TextView topbar_title;
    private int course_id;
    private ChineseCourse chineseCourse;
    private ChineseClass chineseClass;
    //查询到的字
    private List<ChineseWord> chineseWordList;
    private ViewPager viewPage;
    //存放viewPage
    private List<View> viewList;
    //当前页面的字
    private ChineseWord thisChineseWord;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_study);

        topbar_ico = (ImageView) findViewById(R.id.topbar_ico);
        topbar_ico.setOnClickListener(this);
        topbar_title = (TextView) findViewById(R.id.topbar_title);

        viewPage = (ViewPager) findViewById(R.id.viewPage);

        course_id = (int) getIntent().getLongExtra("course_id", 1);
        //课程信息
        chineseCourse = new Select().from(ChineseCourse.class).where("course_id=?", course_id).executeSingle();
        //阶段信息
        if (chineseCourse != null) {
            chineseClass = new Select().from(ChineseClass.class).where("class_id=?", chineseCourse.getClass_id()).executeSingle();
        }

        if (chineseClass != null) {
            topbar_title.setText(chineseClass.getClass_title() + " - " + chineseCourse.getCourse_title());
        }

        //汉字
        chineseWordList = new Select().from(ChineseWord.class).where("course_id=?", course_id).execute();

        //初始化view集合
        viewList = new ArrayList<>();
        //存入view集合
        for (ChineseWord chineseWord : chineseWordList) {
            View view = getLayoutInflater().inflate(R.layout.adapter_pageview, null);
            TextView pageview_text = (TextView) view.findViewById(R.id.pageview_text);
            TextView pageview_text_content = (TextView) view.findViewById(R.id.pageview_text_content);
            pageview_text.setText(chineseWord.getWord_title());
            pageview_text_content.setText(chineseWord.getWord_content());
            viewList.add(view);
        }

        viewPage.setAdapter(new PagerAdapter() {
            @Override
            public int getCount() {
                return chineseWordList.size();
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public Object instantiateItem(View arg0, int arg1) {
                ((ViewPager) arg0).addView(viewList.get(arg1), 0);
                return viewList.get(arg1);
            }

            @Override
            public void destroyItem(View arg0, int arg1, Object arg2) {
                ((ViewPager) arg0).removeView(viewList.get(arg1));
            }

        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.topbar_ico:
                finish();
                break;
        }
    }
}
